package com.black.web.controller.login;

import com.black.common.constant.Constants;
import com.black.common.core.controller.BaseController;
import com.black.common.core.domain.R;
import com.black.common.core.domain.entity.SysMenu;
import com.black.common.core.domain.entity.SysRole;
import com.black.common.core.domain.req.AdminLoginReq;
import com.black.common.core.domain.resp.AdminUserDetails;
import com.black.common.utils.SecurityUtils;
import com.black.framework.web.service.AdminLoginService;
import com.black.system.domain.vo.RouterVo;
import com.black.system.service.SysMenuService;
import com.black.system.service.SysRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author L
 */
@Api(tags = "后台-登录")
@RestController
@RequestMapping("/admin")
public class AdminLoginController extends BaseController {
    @Autowired
    private AdminLoginService adminLoginService;

    @Autowired
    private SysRoleService sysRoleService;

    @Autowired
    private SysMenuService sysMenuService;

    @ApiOperation(value = "登录")
    @PostMapping("/login")
    public R<String> login(@Valid @RequestBody AdminLoginReq req) {
        String token = this.adminLoginService.login(req);
        return R.ok(token);
    }

//    // 退出登录
//    @ApiOperation(value = "退出登录")
//    @PostMapping("/logout")
//    public R<String> logout() {
//        return R.ok();
//    }


    @ApiOperation(value = "获取用户信息")
    @GetMapping("/getInfo")
    public R<Map<String, Object>> getInfo() {
        // 获取用户信息
        AdminUserDetails loginUser = SecurityUtils.getLoginUser();

        //---------------------优化代码，为了满足用户权限更新，在不用重新登录情况下，获取最新的数据-----------------
        // 获取最新的角色集合
        List<SysRole> sysRoleList = this.sysRoleService.findRolesByUserId(loginUser.getUserId());

        //获取最新角色的集合key
        Set<String> roles = sysRoleList.stream().map(SysRole::getRoleKey).collect(Collectors.toSet());

        Set<String> permissions = new HashSet<>();
        if (roles.contains(Constants.SUPER_ADMIN)) {
            permissions.add(Constants.ALL_PERMISSION);
        } else {
            Set<Long> roleIdSet = sysRoleList.stream().map(SysRole::getRoleId).collect(Collectors.toSet());
            // 权限集合
            if (!roleIdSet.isEmpty()) {
                permissions = this.sysMenuService.findPermissionsByRoleIds(roleIdSet);
            }
        }

        // 赋值值，不要让前端用户，产生疑惑
        loginUser.setPermissions(permissions);
        loginUser.getUser().setRoles(sysRoleList);

        //---------------------上面优化代码，为了满足用户权限更新，在不用重新登录情况下，获取最新的数据-----------------

        Map<String, Object> ajax = new HashMap<>();
        ajax.put("user", loginUser);
        ajax.put("roles", roles);
        ajax.put("permissions", permissions);

        return R.ok(ajax);
    }

    @ApiOperation(value = "获取路由信息")
    @GetMapping("/getRouters")
    public R<List<RouterVo>> getRouters() {
        Long userId = SecurityUtils.getUserId();
        // 查询用户拥有的菜单数据
        List<SysMenu> menus = sysMenuService.selectMenuTreeByUserId(userId);
        // 构建菜单树(路由)
        List<RouterVo> routerVos = this.sysMenuService.buildMenus(menus);
        return R.ok(routerVos);
    }

}
